package com.example.algorithm.tree;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author : zhy
 * @since : 2022/7/29
 * 使用广度遍历遍历二叉树
 */
public class MidForeach {

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        if(root==null)
        {
            return list;
        }
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        while(cur!=null||!stack.isEmpty())
        {
           if(cur!=null)
           {
               stack.push(cur);
               cur=cur.left;
           }
           else {
               TreeNode pop = stack.pop();
               list.add(pop.val);
               cur=pop.right;
           }
        }
        return list;
    }

    public static void main(String[] args) {

    }
}
